﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Cache;
using DtoFw;
using DtoFw.Base;
using DtoFw.Query;
using DtoFw.Result;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Service;

namespace SceneEvidenceAPI.Controllers
{
    /// <summary>
    /// 案件
    /// </summary>
    [Route("api/case")]
    [AllowAnonymous]
    public class CaseController : BaseController
    {
        private readonly CaseService _caseService;
        public CaseController(CaseService caseService)
        {
            this._caseService = caseService;
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public BaseResult Post([FromBody] CasePostReqDto reqDto)
        {
            var exist = _caseService.Get(reqDto.CaseName, reqDto.CaseNo);
            if (exist != null)
            {
                return new BaseGenericResult<long>() { Code = DtoFw.Enum.EnumReturnCode.Success, Data = exist.Id, Success = false, Msg = "已存在" };
            }
            return _caseService.Post(reqDto);
        }
        /// <summary>
        /// 新增案件和物品
        /// </summary>
        /// <returns></returns>
        [HttpPost("evidence")]
        public BaseGenericResult<long> PostCaseEvidence([FromBody] CaseEvidencePostReqDto reqDto)
        {
            var exist = _caseService.Get(reqDto.CaseName,reqDto.CaseNo);
            if(exist != null)
            {
                return new BaseGenericResult<long>() { Code = DtoFw.Enum.EnumReturnCode.Success, Data = exist.Id, Success = false, Msg = "已存在" };
            }
            return _caseService.PostCaseEvidence(reqDto);
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        public BaseResult Put([FromBody] CasePutReqDto reqDto)
        {
            return _caseService.Put(reqDto);
        }
        /// <summary>
        /// 查询案件物品
        /// </summary>
        /// <returns></returns>
        [HttpGet("{caseid}/evidence")]
        public BaseGenericResult<CaseEvidenceRespDto> GetCaseEvidence(long caseid) 
        {
            return _caseService.GetCaseEvidence(caseid);
        }
        /// <summary>
        /// 查询案件物品
        /// </summary>
        /// <returns></returns>
        [HttpPost("evidencebyids")]
        public BaseGenericResult<CaseEvidenceRespDto> GetEvidenceByIds([FromBody] BaseQuery reqDto)
        {
            return _caseService.GetEvidenceByIds(reqDto);
        }
        /// <summary>
        /// 单个查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public BaseGenericResult<CaseRespDto> Get([FromQuery] BaseQuery reqDto)
        {
            return _caseService.Get(reqDto);
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <returns></returns>
        [HttpGet("pager")]
        public BaseGenericResult<Pager<CaseRespDto>> Pager([FromQuery]CaseQueryDto queryDto) 
        {
            return _caseService.Pager(queryDto);
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <returns></returns>
        [HttpGet("pager/transfere")]
        public BaseGenericResult<Pager<CaseRespDto>> PagerCaseTransfere([FromQuery] CaseTransfereQueryDto queryDto)
        {
            return _caseService.PagerCaseTransfere(queryDto);
        }
        /// <summary>
        /// 删除案件
        /// </summary>
        /// <returns></returns>
        [HttpDelete]
        public BaseResult DeleteBoxs([FromBody]DeleteDto query) 
        {
            return _caseService.Delete(query);
        }
        /// <summary>
        /// Report top 
        /// </summary>
        /// <returns></returns>
        [HttpGet("home")]
        public BaseGenericResult<CaseHomeRespDto> GetHomeCaseReport()
        {
            var _user = TokenHelper.GetTokenInfo<TokenData>(User);
            return _caseService.GetHomeCaseReport(_user);
        }
        /// <summary>
        /// Report top 
        /// </summary>
        /// <returns></returns>
        [HttpPost("queryCaseHistory")]
        public BaseGenericResult<object> QueryCaseHistory(string JJBH,string ASJBH,string XCKYBH)
        {
            return _caseService.QueryCaseHistory(JJBH, ASJBH, XCKYBH);
        }
        
        #region 案件附件

        /// <summary>
        /// 案件附件查询
        /// </summary>
        /// <returns></returns>
        [HttpPost("pager/caseattach")]
        public BaseGenericResult<Pager<Domain.Entity.CaseAttachFileEntity>> PagerCaseAttachFile(CaseAttachReqDto inf)
        {
            return _caseService.QueryCaseAttach(inf);
        }
        /// <summary>
        /// 删除案件附件
        /// </summary>
        [HttpDelete("delete/caseattach")]
        public BaseGenericResult<string> DeleteCaseAttachFile(CaseAttachReqDto inf)
        {
            return _caseService.DeleteCaseAttachFile(inf);
        }
        /// <summary>
        /// 案件附件查询
        /// </summary>
        /// <returns></returns>
        [HttpPost("matchEvidence")]
        public BaseGenericResult<string> MatchEvidence(CaseAttachReqDto inf)
        {
            return _caseService.MatchEvidence(inf);
        }
        /// <summary>
        /// 案件附件查询
        /// </summary>
        /// <returns></returns>
        [HttpPost("cancelmatch/caseattach")]
        public BaseGenericResult<string> CancelMatchCaseAttach(CaseAttachReqDto inf)
        {
            return _caseService.CancelMatchEvidence(inf);
        }
        #endregion 案件附件
    }
}
